package cn.wit.summit.chwan.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.adapters.MediaTestAdapter;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.models.event.DownloadEvent;
import cn.wit.summit.chwan.models.response.MediaTestResponse;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.tools.downloadtool.DownLoadStatusConfig;
import cn.wit.summit.chwan.ui.base.BaseActivity;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.widgets.DownloadViewStrokeV2;
import retrofit2.Retrofit;

/**
 * Created by Alan on 2018/7/19.
 */

public class MediaTestActivty extends BaseActivity {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private String titleStr;
    private int mPosition;

    private MediaTestAdapter adapter;
    private List<MediaTestAdapter.ViewBean> mDatas = new ArrayList<>();
    private LinearLayoutManager layoutManager;

    private RetrofitManager retrofitManager;
    private Retrofit retrofit;
    private ApiUtil api;

    private Map<Integer, Integer> mapPo = new HashMap<>();
    private int mapPoIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        actionBarStyle = ACTION_BAR_NORMAL;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activty_media_test);
        ButterKnife.bind(this);

        titleStr = getIntent().getStringExtra("title");
        String po = getIntent().getStringExtra("mPosition");
        if (StringUtils.isEmpty(po)) {
            mPosition = 0;
        } else {
            mPosition = Integer.valueOf(po);
        }
        LogUtils.e("tbl>>>>mPosition:"+mPosition);
        setBarTitleTx(titleStr);

        retrofitManager = RetrofitManager.getInstance(mContext);
        retrofit = retrofitManager.getRetrofit();
        api = retrofit.create(ApiUtil.class);

        initListView();
        getData();
    }

    private void initListView() {
        adapter = new MediaTestAdapter(mAty, mDatas);
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    private void getData() {
        api.getMediaTest(retrofitManager.mediaTestRequest()).enqueue(new MyCallback<ResponseBase<MediaTestResponse>>() {
            @Override
            public void onSuccess(ResponseBase<MediaTestResponse> model) {
                updataUi(model.getData_info());
            }

            @Override
            public void onFailure(int code, String msg) {
                showToast(msg);
            }
        });
    }

    private void updataUi(MediaTestResponse data_info) {
        if (data_info==null) return;

        mapPoIndex = 0;
        mDatas.clear();
        mapPo.clear();
        adapter.clearTempData();

        JumpBaseBean googleList = data_info.getGoogle_list();
        if (googleList!=null) {
            mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_TOP, googleList));
            mapPo.put(mapPoIndex, mDatas.size()-1);
            for (int i=0;googleList.getGame_list()!=null && i<googleList.getGame_list().size();i++) {
                if (i==(googleList.getGame_list().size()-1)) {
                    mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_GAME, googleList.getGame_list().get(i), googleList.getJump_model_type(), true));
                } else {
                    mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_GAME, googleList.getGame_list().get(i), googleList.getJump_model_type()));
                }
            }
            mapPoIndex += 1;
        }

        JumpBaseBean appleList = data_info.getApple_list();
        if (appleList!=null) {
            mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_TOP, appleList));
            mapPo.put(mapPoIndex, mDatas.size()-1);
            for (int i=0;appleList.getGame_list()!=null && i<appleList.getGame_list().size();i++) {
                if (i==(appleList.getGame_list().size()-1)) {
                    mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_GAME, appleList.getGame_list().get(i), appleList.getJump_model_type(), true));
                } else {
                    mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_GAME, appleList.getGame_list().get(i), appleList.getJump_model_type()));
                }
            }
            mapPoIndex += 1;
        }

        JumpBaseBean tagList = data_info.getTag_list();
        if (tagList!=null) {
            mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_TOP, tagList));
            mapPo.put(mapPoIndex, mDatas.size()-1);
            for (int i=0;tagList.getGame_list()!=null && i<tagList.getGame_list().size();i++) {
                if (i==(tagList.getGame_list().size()-1)) {
                    mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_GAME, tagList.getGame_list().get(i), tagList.getJump_model_type(), true));
                } else {
                    mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_GAME, tagList.getGame_list().get(i), tagList.getJump_model_type()));
                }
            }
            mapPoIndex += 1;
        }

        JumpBaseBean ttList = data_info.getTt_list();
        if (ttList!=null) {
            mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_TOP, ttList));
            mapPo.put(mapPoIndex, mDatas.size()-1);
            for (int i=0;ttList.getGame_list()!=null && i<ttList.getGame_list().size();i++) {
                if (i==(ttList.getGame_list().size()-1)) {
                    mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_GAME, ttList.getGame_list().get(i), ttList.getJump_model_type(), true));
                } else {
                    mDatas.add(new MediaTestAdapter.ViewBean(MediaTestAdapter.ViewType.TYPE_GAME, ttList.getGame_list().get(i), ttList.getJump_model_type()));
                }
            }
            mapPoIndex += 1;
        }

        adapter.notifyDataSetChanged();

        if (mapPo.containsKey(mPosition) && mPosition <= mapPo.size()-1) {
            LogUtils.e("tbl>>>>mapPo.get(mPosition):"+mapPo.get(mPosition));
            recyclerView.scrollToPosition(mapPo.get(mPosition));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(DownloadEvent downloadEvent) {
//        recommendTopDownloadTop.setTotalDownloadNum();//设置下载数

        String str = adapter.getPoByGameId(downloadEvent.getGameId());
        if (!StringUtils.isEmpty(str)) {
            GameListBaseBean bean = adapter.getBeanData(Integer.valueOf(str), 0);
            if (bean == null) return;
            DownLoadTaskTable table = bean.getTaskTable();
            if (table == null) {
                table = bean.getDownloadTaskTableSimple();
            }
            table.setDownStatus(downloadEvent.status);
            if (downloadEvent.getStatus() == DownLoadStatusConfig.DOWNLOADING) {
                table.setDownprogress(downloadEvent.getProgress());
            }

            View view = layoutManager.findViewByPosition(Integer.valueOf(str));

            DownloadViewStrokeV2 dowView = view.findViewById(R.id.itemDownloadBtn);
            if (dowView != null) {
                dowView.updateButn(table);
            }
        }
    }

    @Override
    public void reGetData() {
        super.reGetData();
        getData();
    }
}
